import math
import pygame

def calculate_intersection(segment1, segment2):
    # 线段1的起点和终点
    P1, P2 = segment1[0], segment1[1]
    # 线段2的起点和终点
    P3, P4 = segment2[0], segment2[1]

    # 计算斜率
    m1 = (P2[1] - P1[1]) / (P2[0] - P1[0]) if (P2[0] - P1[0]) != 0 else None
    m2 = (P4[1] - P3[1]) / (P4[0] - P3[0]) if (P4[0] - P3[0]) != 0 else None

    # 判断线段关系
    if m1 is None and m2 is None:
        # 两条线段都是垂直线段且平行，无交点
        return None
    elif m1 is None:
        # 线段1是垂直线段，线段2不是
        intersection_x = P1[0]
        intersection_y = m2 * (intersection_x - P3[0]) + P3[1]
        return (intersection_x, intersection_y)
    elif m2 is None:
        # 线段2是垂直线段，线段1不是
        intersection_x = P3[0]
        intersection_y = m1 * (intersection_x - P1[0]) + P1[1]
        return (intersection_x, intersection_y)
    elif m1 == m2:
        # 两条线段平行，无交点
        return None
    else:
        # 计算交点的 x 坐标
        intersection_x = (m1 * P1[0] - m2 * P3[0] - P1[1] + P3[1]) / (m1 - m2)
        # 计算交点的 y 坐标
        intersection_y = m1 * (intersection_x - P1[0]) + P1[1]
        return (intersection_x, intersection_y)

class Game:
    def __init__(self):
        self.player1 = "Player 1"
        self.player2 = "Player 2"
        self.current_player = self.player1
        self.done = False
        self.won = False
        self.pieces = []  # 棋子列表

        # 初始化 Pygame 库
        pygame.init()

        # 显示设置
        self.size = [600, 600]
        self.green = (0, 155, 0)
        self.black = (0, 0, 0)
        self.white = (255, 255, 255)
        self.screen = pygame.display.set_mode(self.size)

        # ...（省略其他初始化代码）

    def run_game(self):
        pygame.init()

        # 计算窗口尺寸和五角星半径
        width, height = self.size
        radius = min(width, height) * 0.9 / 2

        points5star = self.draw_board(radius)  # 绘制初始棋盘

        print(radius, points5star)  # debug info output.

        # 绘制调试信息
        font = pygame.font.Font(None, 60)
        for i, point in enumerate(points5star):
            index_text = font.render(str(i), True, pygame.Color("red"))
            self.screen.blit(index_text, (point[0], point[1]))

        pygame.display.flip()

        while not self.done:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.done = True
                    break
                elif event.type == pygame.MOUSEBUTTONUP:
                    pos = pygame.mouse.get_pos()
                    self.handle_click(pos)

    def handle_click(self, pos):
        # 处理鼠标点击事件
        for piece in self.pieces:
            if piece.cx - piece.radius <= pos[0] <= piece.cx + piece.radius \
                    and piece.cy - piece.radius <= pos[1] <= piece.cy + piece.radius:
                self.handle_piece_click(piece)
                break

    def handle_piece_click(self, piece):
        # 处理棋子点击事件
        if piece.color == self.current_player:
            self.move_piece(piece)

    def move_piece(self, piece):
        # 处理棋子移动
        if self.last_move is not None:
            self.last_move.flip_color()

        dest = self.get_clicked_position()

        if piece.can_move_to(dest):
            piece.move(dest)
            self.last_move = piece
            self.switch_player()
            if self.check_for_winner():
                self.done = True

        self.draw_board()

    def get_clicked_position(self):
        # 根据鼠标点击的位置，获取对应的棋盘位置
        for pos, (cx, cy) in self.POSITION.items():
            if cx - piece.radius <= pos[0] <= cx + piece.radius \
                    and cy - piece.radius <= pos[1] <= cy + piece.radius:
                return pos
        return None

    def switch_player(self):
        # 切换玩家
        if self.current_player == self.player1:
            self.current_player = self.player2
        else:
            self.current_player = self.player1

    def check_for_winner(self):
        # 根据游戏规则判断是否有胜利者
        # 如果有胜利者，返回 True
        # 如果没有胜利者，返回 False
        if len(self.pieces) == 0:
            self.won = True
            return True
        return False

    def draw_board(self, radius):
        # 填充背景色为绿色
        self.screen.fill(self.green)

        # 计算五角星的顶点坐标和交点坐标
        star_points = []  # 五角星的顶点坐标
        intersection_points = []  # 五条线段相交的交点坐标

        angle = math.radians(360 / 5)  # 五角星每个角的角度

        # 计算顶点坐标
        for i in range(5):
            x = radius * math.cos(i * angle - math.radians(90))  # 左旋转90度
            y = radius * math.sin(i * angle - math.radians(90))  # 左旋转90度
            star_points.append((300 + x, 300 + y))

        # 绘制顶点连线
        for i in range(5):
            pygame.draw.line(self.screen, self.black, star_points[i], star_points[(i+2) % 5], 3)

        # 计算交点坐标
        for i in range(5):
            intersection_segment = [star_points[i], star_points[(i+2) % 5]]
            intersection = calculate_intersection(intersection_segment, [star_points[(i+1) % 5], star_points[(i+3) % 5]])
            if intersection is not None:
                intersection_points.append(intersection)

        pygame.display.flip()

        return star_points + intersection_points


    def main(self):
        self.run_game()
        pygame.quit()  # 在游戏主循环结束后关闭 Pygame 库

if __name__ == "__main__":
    game = Game()
    game.main()